// pagesa/Head_store/Head_store.js
import {
    getProductslist,
    spreadstoreposter
} from '../../api/store.js';
import {
    userShare
} from '../../api/user.js';
const app = getApp();
Page({

    /**
     * 页面的初始数据
     */
    data: {
        parameter: {
            'navbar': '1',
            'return': '1',
            'title': '团长店铺'
        },
        imgUrls: [
            '../../images/writeOffBg.jpg',
            '../../images/writeOffBg.jpg',
            '../../images/writeOffBg.jpg'
        ],
        indicatorDots: true, // 是否显示面板指示点
        autoplay: false, // 是否自动切换
        circular: true, // 是否采用衔接滑动
        interval: 3000, // 自动切换时间间隔
        duration: 1000, // 滑动动画时长
        shopList: [],
        loading: false,
        loadend: false,
        loadTitle: '加载更多',
        currentData:1,
        rebate:0,
        sales:0,
        nows: true,
        news:0,
        where: {
            rebateOrder: '',
            salesOrder: '',
            page: 1,
            limit: 999,
            is_spread: 1,
        },
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        this.getProductslist();
        this.spreadstoreposter();
    },
    
    spreadstoreposter: function() {
        spreadstoreposter().then(res => {
            this.setData({
                imgUrls: res.data.banner,
                poster1: res.data.poster1,
                poster2: res.data.poster2
            })
        });
    },
    set_sort:function(e){
        var dataset = e.target.dataset;
        var page = this.data.page;
        switch (dataset.type) {
            case '1':
                this.setData({
                    rebate: 0,
                    sales: 0,
                    nows: true
                });
                break;
            case '2':
                if (this.data.rebate == 0)
                    this.data.rebate = 1;
                else if (this.data.rebate == 1)
                    this.data.rebate = 2;
                else if (this.data.rebate == 2)
                    this.data.rebate = 0;
                this.setData({
                    rebate: this.data.rebate,
                    nows:false,
                    sales: 0
                });
                break;
            case '3':
                if (this.data.sales == 0)
                    this.data.sales = 1;
                else if (this.data.sales == 1)
                    this.data.sales = 2;
                else if (this.data.sales == 2)
                    this.data.sales = 0;
                this.setData({
                    sales: this.data.sales,
                    nows: false,
                    rebate: 0
                });
                break;
        }
        this.setData({
            loadend: false,
            ['where.page']: 1
        });
        this.getProductslist(true);
    },
    //设置where条件
    setWhere: function () {
        if (this.data.rebate == 0)
            this.data.where.rebateOrder = '';
        else if (this.data.rebate == 1)
            this.data.where.rebateOrder = 'desc';
        else if (this.data.rebate == 2)
            this.data.where.rebateOrder = 'asc';
        if (this.data.sales == 0)
            this.data.where.salesOrder = '';
        else if (this.data.sales == 1)
            this.data.where.salesOrder = 'desc';
        else if (this.data.sales == 2)
            this.data.where.salesOrder = 'asc';
        this.data.news = this.data.nows ? 1 : 0;
        this.setData({
            where: this.data.where
        });
    },
    getProductslist: function (isPage) {
        var that = this
        this.setWhere();
        if (that.data.loading) return;
        if (that.data.loadend) return;
        if (isPage === true) that.setData({
            shopList: []
        });
        that.setData({
            loading: true,
            loadTitle: ''
        });
        getProductslist(that.data.where).then(res => {
            var list = res.data;
            let shopList = app.SplitArray(list, that.data.shopList);
            let loadend = list.length < that.data.where.limit;
            this.setData({
                list: shopList,
                loadend: loadend,
                loadTitle: loadend ? '我也是有底线的' : '加载更多',
                ['where.page']: that.data.where.page + 1,
                loading: false,
            })
        }).catch(err => {
            that.setData({
                loading: false,
                loadTitle: '加载更多'
            });
        });
    },
    Jump: function (e) {
        console.log(e)
        wx.navigateTo({
            url: '/pages/goods_details/index?id=' + e.currentTarget.dataset.id,
        })
    },
   
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {
        this.getProductslist();
    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function(e) {
        userShare();
        return {
            title: e.target.dataset.store_name || '',
            imageUrl: e.target.dataset.image || '',
            path: '/pages/goods_details/index?id=' + e.target.dataset.id + '&spid=' + app.globalData.uid + '&isshare=' + 1,
        }
    }
})